package com.ahora.org.controller.biometrico.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MostrarFoto
 */
@WebServlet("/ServletLogotipo")
public class ServletMostrarLogo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String rutaLogo = request.getParameter("rutaLogo");
		ServletOutputStream out = response.getOutputStream();
		try {
			
			File f = new File(rutaLogo); 
			response.setContentType(getTipoContentType(rutaLogo));
			InputStream is = new FileInputStream (f);
			int length = rutaLogo.length();
			int bufferSize = 1024;
			byte[] buffer = new byte[bufferSize];
			while ((length = is.read(buffer)) != -1) {
				out.write(buffer, 0, length);
			}
			is.close();
			out.flush();
		} catch (Exception e) {
			response.setContentType("text/html");
			out.println("<html><head><title>No hay imagen disponible</title></head>");
			out.println("<body><h4><font color='red'>Image Display Error="
					+ e.getMessage() + "</font></h4></body></html>");
			return;
		}
	}
	
	private String getTipoContentType(String contenttype) {
		String responseType = "";
		if (contenttype.indexOf("doc") > -1) {
			responseType = "application/vnd.ms-word";
		}
		if (contenttype.indexOf("rtf") > -1) {
			responseType = "application/rtf";
		} else if (contenttype.indexOf("xls") > -1) {
			responseType = "application/vnd.ms-excel";
		} else if (contenttype.indexOf("ppt") > -1) {
			responseType = "application/ppt";
		} else if (contenttype.indexOf("xml") > -1) {
			responseType = "text/xml";
		} else if (contenttype.indexOf("html") > -1) {
			responseType = "text/html";
		} else if (contenttype.indexOf("htm") > -1) {
			responseType = "text/htm";
		} else if (contenttype.indexOf("txt") > -1
				|| contenttype.indexOf("log") > -1) {
			responseType = "text/plain";
		} else if (contenttype.indexOf("pdf") > -1) {
			responseType = "application/pdf";
		} else if (contenttype.indexOf("gif") > -1) {
			responseType = "image/gif";
		} else if (contenttype.indexOf("png") > -1) {
			responseType = "image/png";
		} else if (contenttype.indexOf("jpg") > -1) {
			responseType = "image/jpg";
		} else if (contenttype.indexOf("jpeg") > -1) {
			responseType = "image/JPEG";
		} else if (contenttype.indexOf("jpe") > -1) {
			responseType = "image/JPEG";
		} else if (contenttype.indexOf("bmp") > -1) {

			responseType = "image/bmp";
		} else if (contenttype.indexOf("jpeg") > -1) {
			responseType = "image/JPEG";
		} else if (contenttype.indexOf("tiff") > -1) {
			responseType = "image/tiff";
		} else if (contenttype.indexOf("tif") > -1) {
			responseType = "image/tiff";
		} else if (contenttype.indexOf("html") > -1) {
			responseType = "text/html";
		} else {
			responseType = "application/octet-stream";
		}
		return responseType;

	}
}
